package com.libservice.models

import com.google.gson.annotations.Expose
import com.libbase.MapCodable
import com.libbase.extension.appVersion
import com.libbase.utils.RomUtils
import com.libui.BaseApplication
import java.util.Date
import java.util.UUID

data class DeviceModel(
    @Expose val id: String = UUID.randomUUID().toString(),
    @Expose val date: Date = Date(),
    @Expose var userAgreementVersion: String? = null,
    @Expose var privacyAgreementVersion: String? = null,
    @Expose var guideVersion: String? = null,
    @Expose(serialize = true, deserialize = false)
    val appVersion: String =
        BaseApplication.instance?.run {
            packageManager.appVersion(packageName)
        } ?: "",
    @Expose(serialize = true, deserialize = false)
    val systemInfo: String = run {
        val info = "Android${android.os.Build.VERSION.RELEASE}" +
                ";${android.os.Build.VERSION.SDK_INT}" +
                ";${android.os.Build.MODEL}" +
                ";${android.os.Build.MANUFACTURER}" +
                ";${RomUtils.name ?: ""}" +
                ";${RomUtils.version ?: ""}"

        if (RomUtils.isHarmonyOs) info + ";Harmony${RomUtils.harmonyVersion}" else info
    },
) : MapCodable {
    companion object {
        fun create(map: Map<String, Any>): DeviceModel? {
            val id: String = map["id"] as? String ?: return null
            val date: Long = map["date"] as? Long ?: return null

            return DeviceModel(
                id,
                Date(date),
                map["userAgreementVersion"] as? String,
                map["privacyAgreementVersion"] as? String,
                map["guideVersion"] as? String,
            )
        }
    }

    override fun toMap(): Map<String, Any> {
        val map = mutableMapOf<String, Any>(
            "id" to id,
            "date" to date.time,
            "appVersion" to appVersion,
            "systemInfo" to systemInfo,
        )

        userAgreementVersion?.let {
            map["userAgreementVersion"] = it
        }

        privacyAgreementVersion?.let {
            map["privacyAgreementVersion"] = it
        }

        guideVersion?.let {
            map["guideVersion"] = it
        }

        return map
    }
}